package com.gao.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gao.service.UserService;
import com.gao.service.impl.UserServiceImpl;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//??????????
		String username = request.getParameter("username");
		System.out.println("?????????username="+username);
		String password = request.getParameter("password");
		System.out.println("?????????password="+password);
		String phonenum = request.getParameter("phonenum");
		System.out.println("?????????phonenum="+phonenum);
		
		
		//???????
		UserService userService= new UserServiceImpl();
		boolean flag = false;
		try {
			flag = userService.register(username, password, phonenum);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		if(flag){//??????????????????
			response.sendRedirect("login.jsp");
		}else{
			request.setAttribute("msg", "??????????????????????");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
		
		
		
		
	}

}
